{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b73850d2",
   "metadata": {},
   "source": [
    "* 日期：2022.10.26\n",
    "* 记录人：liyuanyuan"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e1ba894",
   "metadata": {},
   "source": [
    "## 购物车程序项目：\n",
    "> 1. 运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户\n",
    "> 2. 让用户输入商品编号进行商品的购买\n",
    "> 3. 用户选择商品后，检查用户的余额是否足够，若够则直接扣款，不够则提醒用户\n",
    "> 4.用户可以随时退出购买，退出时打印用户以购买的商品和支付宝余额"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "24c0b4f1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额：200\n",
      "------商品列表------\n",
      "商品编号:0 ['自行车', 299]\n",
      "商品编号:1 ['iphone 14 pro', 8000]\n",
      "商品编号:2 ['NFT 盲盒', 28]\n",
      "商品编号:3 ['许老师的python书', 80]\n",
      "请输入商品编号：2\n",
      "您购买的商品已增加到购物车，您的余额还有： 172\n",
      "------商品列表------\n",
      "商品编号:0 ['自行车', 299]\n",
      "商品编号:1 ['iphone 14 pro', 8000]\n",
      "商品编号:2 ['NFT 盲盒', 28]\n",
      "商品编号:3 ['许老师的python书', 80]\n",
      "请输入商品编号：3\n",
      "您购买的商品已增加到购物车，您的余额还有： 92\n",
      "------商品列表------\n",
      "商品编号:0 ['自行车', 299]\n",
      "商品编号:1 ['iphone 14 pro', 8000]\n",
      "商品编号:2 ['NFT 盲盒', 28]\n",
      "商品编号:3 ['许老师的python书', 80]\n",
      "请输入商品编号：3\n",
      "您购买的商品已增加到购物车，您的余额还有： 12\n",
      "------商品列表------\n",
      "商品编号:0 ['自行车', 299]\n",
      "商品编号:1 ['iphone 14 pro', 8000]\n",
      "商品编号:2 ['NFT 盲盒', 28]\n",
      "商品编号:3 ['许老师的python书', 80]\n",
      "请输入商品编号：3\n",
      "您的余额已不足，请更换支付方式\n",
      "------商品列表------\n",
      "商品编号:0 ['自行车', 299]\n",
      "商品编号:1 ['iphone 14 pro', 8000]\n",
      "商品编号:2 ['NFT 盲盒', 28]\n",
      "商品编号:3 ['许老师的python书', 80]\n",
      "请输入商品编号：n\n",
      "您输入的商品不存在或输入有误，请重新尝试\n",
      "------商品列表------\n",
      "商品编号:0 ['自行车', 299]\n",
      "商品编号:1 ['iphone 14 pro', 8000]\n",
      "商品编号:2 ['NFT 盲盒', 28]\n",
      "商品编号:3 ['许老师的python书', 80]\n",
      "请输入商品编号：q\n",
      "您的购物车： [['NFT 盲盒', 28], ['许老师的python书', 80], ['许老师的python书', 80]]\n",
      "您的余额还有： 12\n"
     ]
    }
   ],
   "source": [
    "# 准备工作\n",
    "product_list = [\n",
    "    [\"自行车\",299],\n",
    "    [\"iphone 14 pro\",8000],\n",
    "    [\"NFT 盲盒\",28],\n",
    "    [\"许老师的python书\",80]\n",
    "]\n",
    "shopping_car = []\n",
    "Alipay = int(input(\"请输入余额：\"))\n",
    "while True:\n",
    "    print(\"------商品列表------\")\n",
    "    for i in range(4):\n",
    "        print(\"商品编号:\"+str(i),product_list[i])\n",
    "    user_choice = input(\"请输入商品编号：\")\n",
    "    # 情景1：用户输入了商品编号：shopping\n",
    "    if user_choice.isdigit():\n",
    "        # 判断钱够不够，商品是谁？=>product_list[user_choice][i]\n",
    "        if Alipay >= product_list[int(user_choice)][1]:\n",
    "            Alipay = Alipay - product_list[int(user_choice)][1] # 买完商品剩余的钱\n",
    "            shopping_car.append(product_list[int(user_choice)])\n",
    "            print(\"您购买的商品已增加到购物车，您的余额还有：\",Alipay)\n",
    "        else:\n",
    "            print(\"您的余额已不足，请更换支付方式\")\n",
    "    # 情景2：用户输入\"q\",退出购买\n",
    "    elif user_choice == \"q\":\n",
    "        print(\"您的购物车：\",shopping_car)\n",
    "        print(\"您的余额还有：\",Alipay)\n",
    "        break\n",
    "    else:\n",
    "        print(\"您输入的商品不存在或输入有误，请重新尝试\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "469b6d2b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7e37f837",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
